// pages/myback/myback.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ani: null,
    num: 1,
    check: -1,
    active: 0,
    list: [],
    checkNum: 0,
    price: [
      '不限',
      '100万以内',
      '100-200万',
      '200-300万',
      '300-500万',
      '500万以上'
    ],
    itl: [
      '不限',
      '40㎡以下',
      '40㎡-60㎡',
      '60㎡-90㎡',
      '90㎡-120㎡',
      '120㎡-144㎡',
      '144㎡以上',
    ],
    checks: [0, 0],
    value1: 0,
    value2: 'a',
    state: {
      province: false,
      city: false,
      area: false
    },
    val: {
      province: null
    },
    actProvince: ['北京市'],
    actCity: ['北京市', '北京市'],
    actArea: [110000, 110100, 110101],
    page_token: 0
  },
  timeReset() {
    this.setData({
      page_token: 0
    })
    var data = {
      page_token: 0,
      page_size: 10
    }
    this.api(data)
    this.selectComponent('#item3').toggle();
  },
  onReachBottom() { //上拉加载更多
    var that = this;
    //检查是否还有数据可以加载
    var total_size = that.data.list.total_size;
    console.log(that.data.list)
    var size = that.data.list.results.length;
    var page_token = that.data.page_token
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size: 10,
        ordertype: that.data.active,
        page_token: page_token += 1
      }
      that.api(data)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true
      })
    }
    that.setData({
      page_token: page_token
    })
    console.log(size, '===', total_size)
    console.log(that.data.fpmyl)
  },
  preventdefault: function () {},
  authority(e) { //全选
    var that = this;
    var list = that.data.list;
    var checked = e.detail;
    for (var i = 0; i < that.data.list.length; i++) {
      if (checked == true) {
        that.data.list[i].check = true
      } else {
        that.data.list[i].check = false
      }
    }
    that.setData({
      list: list,
      checked: checked
    })
  },
  operating(e) {
    var that = this;
    var list = that.data.list;
    var active = that.data.active;
    var states = ['', 2, 1]
    console.log(states[active])
    var id = [];
    for (var i = 0; i < list.results.length; i++) {
      if (list.results[i].check == true) {
        // id = list.results[i].id;
        // console.log(list.results[i].id)
        id.push(list.results[i].id)
      }
    }
    // var data = {
    //   id: id,
    //   status: states[active]
    // };
    console.log(id)
    let params = {
      ids:id,
      status:states[active]
    }
    getApp().ports.plsxj({data: params}).then((res) => {
      var res = res.data;
      var obj = {
        status: active
      }
      that.api(obj)
      that.setData({
        active: that.data.active
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  search(e) { //搜索
    console.log(e.detail)
    var data = {
      title: e.detail,
      page_size: 10,
      page_token: 0
    };
    this.api(data)
  },
  choose(e) {
    var that = this;
    console.log('单选')
    var index = e.detail.currentTarget.dataset.index;
    var list = that.data.list;
    var checkNum = that.data.checkNum;
    console.log(list)
    if (list.results[index].check === true) {
      list.results[index].check = false
      checkNum -= 1
    } else {
      list.results[index].check = true;
      checkNum += 1
    }
    if (checkNum == list.results.length) {
      that.data.checked = true
    } else {
      that.data.checked = false
    }
    that.setData({
      list: list,
      checkNum: checkNum,
      checked: that.data.checked
    })
  },
  delete(e) {
    var that = this;
    var list = that.data.list;
    var active = that.data.active;
    var states = ['', 2, 1]
    var id = [];
    id[0] = e.detail;
    wx.showModal({
      title: '提示',
      content: '是否删除房源',
      success(res) {
        if (res.confirm) {
          getApp().ports.plscfy({
            data: {ids: id}
          }).then((res) => {
            var res = res.data;
            var obj = {
              status: active
            }
            that.api(obj)
            that.setData({
              active: that.data.active
            })
          }).catch(() => {
            console.error('出啥错了？');
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  api(data) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    if (data.status == 0) delete(data.status)
    getApp().ports.api5({
      data: data
    }).then((res) => {
      var res = res.data;
      var statuss = ['待审核', '待上架', '出售中', '已出售'];
      console.log(res.results, '成功');
      if (that.data.page_token >= 1) {
        res.results.forEach(item => {
          item.check = false;
          item.active = that.data.active;
          item.identity = that.data.identity
          item.type = statuss[item.status];
          item.url = '../../subpackages/pages/listing/listing?typeId=' + item.id
          that.data.list.results.push(item)
        })
        res.results.checked = false;
        res.results = that.data.list.results
      } else {
        res.results.forEach(item => {
          item.check = false;
          item.active = that.data.active;
          item.identity = that.data.identity
          item.type = statuss[item.status];
          item.url = '../../subpackages/pages/listing/listing?typeId=' + item.id
        })
        res.results.checked = false;
      }
      that.setData({
        list: res,
        myl: false
      })
      wx.hideLoading({
        success: (res) => {},
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  showEdit(e) {
    var that = this;
    var list = that.data.list;
    list.forEach(item => {
      item.check = false;
    })
    that.setData({
      list: list
    })
  },
  add() {
    console.log('创建房源')
    wx.showModal({
      title: '提示',
      content: '是否直接创建折扣房源',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.navigateTo({
            url: '../../subpackages/pages/listing/listing?direct=1',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      active_color: app.globalData.active_color,
      identity: wx.getStorageSync('identity'),
      imageRoot: app.globalData.imageRoot
    })
    var data = {
      page_size: 10,
      page_token: 0
    }
    this.api(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    console.log('下拉刷新')
    this.setData({
      page_token: 0
    })
    that.api({
      status: that.data.active 
    })
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },



 
  areaInEnter(e) {
    var that = this;
    var status = that.data.active;
    var reg = /[\u4e00-\u9fa5]/g;
    var time = e.detail.years + '-' + e.detail.months + '-' + e.detail.days;
    var create_time = time.replace(reg, "")
    var data = {
      status: status,
      create_time: create_time,
      page_size: 10
    }
    that.api(data)
    that.selectComponent('#item3').toggle();
  },
  check(e) {
    console.log(e)
    var that = this;
    var status = e.currentTarget.dataset.status;
    var checks = that.data.checks;
    checks[status] = e.currentTarget.dataset.index;
    that.setData({
      checks: checks
    })
  },
  onChange(event) { //分类筛选
    var that = this;
    var index = event.detail.index;
    if (index === 0) {
      index = ''
    }
    that.setData({
      active: index,
      checked: false,
      page_token: 0,
      myl: false
    })
    var data = {
      status: index,
      page_size: 10
    }
    that.api(data)
  },
  itemClick: function (e) {
    this.setData({
      num: e.currentTarget.dataset.num
    })
  },
  Price(e) { //筛选价格
    var that = this;
    var status = e.currentTarget.dataset.status;
    var currentTab = that.data.active;
    var checks = that.data.checks;
    var lt = 0;
    var gt = 0;
    var sales = '';
    console.log(that.data.range);
    if (checks[0] == 1) {
      gt = 0;
      lt = 1000000;
    } else if (checks[0] == 2) {
      gt = 1000000;
      lt = 2000000;
    } else if (checks[0] == 3) {
      gt = 2000000;
      lt = 3000000;
    } else if (checks[0] == 4) {
      gt = 3000000;
      lt = 5000000;
    } else if (checks[0] == 5) {
      gt = 5000000;
    }
    var data = {
      'priceMax': lt, //小于
      'priceMin': gt, //大于
      page_size: 10,
      page_token: 0
    };
    that.api(data)
    that.selectComponent('#item2').toggle();
  },
  More(e) { //面积筛选
    var that = this;
    var checks = that.data.checks;
    var lt = 0;
    var gt = 0;
    if (checks[1].check == 1) {
      gt = 0;
      lt = 40;
    } else if (checks[1].check == 2) {
      gt = 40;
      lt = 60;
    } else if (checks[1].check == 3) {
      gt = 60;
      lt = 30;
    } else if (checks[1].check == 4) {
      gt = 90;
      lt = 120;
    } else if (checks[1].check == 5) {
      gt = 120;
      lt = 144;
    } else if (checks[1].check == 6) {
      gt = 144;
    }
    var data = {
      'areaMax': lt,
      'areaMin': gt,
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    that.selectComponent('#item4').toggle();
  },
  areaReset(e) {
    let that = this;
    that.setData({
      page_token: 0
    })
    var data = {
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    that.selectComponent('#item1').toggle();
  },
  inEnter(e) {
    var that = this;
    var status = that.data.active;
    console.log(e.detail);
    var district = (e.detail.area.value == '全市' ? '' : e.detail.area.value);
    var city = (e.detail.city.value == '全省' ? '' : e.detail.city.value);
    var province = (e.detail.province.value == '全国' ? '' : e.detail.province.value);
    var data = {
      status: status,
      province: province,
      city: city,
      district: district,
      page_size: 10,
      page_token: 0
    }
    that.api(data)
    that.selectComponent('#item1').toggle();
  },
})